/*
 *   Copyright 2008 Luca Bellonda <lbellonda@gmail.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as
 *   published by the Free Software Foundation; either version 2 or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <QTime>
#include <QtDBus/qdbusconnection.h>
#include <QDBusConnection>
#include "dbusengine.h"
#include "plasma/datacontainer.h"
#include <QDomDocument>

#ifdef DUMP
#define   D(x)    x
#else
#define   D(x)
#endif


DBusEngine::DBusEngine(QObject* parent, const QVariantList& args)
        : Plasma::DataEngine(parent, args)
{
    Q_UNUSED(args)
    m_sources.append(SOURCE_SYSTEM_SERVICES);
    m_sources.append(SOURCE_SESSION_SERVICES);
    setMinimumPollingInterval(1000);
}

DBusEngine::~DBusEngine()
{
}

void DBusEngine::init()
{
    scan();
}

QStringList DBusEngine::sources() const
{
    return m_sources;
}

bool DBusEngine::sourceRequestEvent(const QString &name)
{
    return updateSourceEvent(name);
}

void DBusEngine::serviceChangedSession(const QString &)
{
    updateSourceEvent(SOURCE_SESSION_SERVICES);
}

void DBusEngine::serviceChangedSystem(const QString &)
{
    updateSourceEvent(SOURCE_SYSTEM_SERVICES);
}

bool DBusEngine::updateSourceEvent(const QString &name)
{
    scan();
    if (name == I18N_NOOP(SOURCE_SESSION_SERVICES)) {
        setData(name, I18N_NOOP("services"), QVariant(m_lastDataSession));
        return true;
    }
    if (name == I18N_NOOP(SOURCE_SYSTEM_SERVICES)) {
        setData(name, I18N_NOOP("services"), QVariant(m_lastDataSystem));
        return true;
    }

    return false ;
}


void DBusEngine::scan()
{
    connect(&m_thread, SIGNAL(scanEnd()), this, SLOT(scanEnd()));
    D(printf("inizio thread\n"));
    m_thread.scan();
    D(printf("thread invoked\n"));
}


void DBusEngine::scanEnd()
{
    D(printf("SCAN END **********************************************************************\n"));
    m_thread.getData(m_lastDataSystem, m_lastDataSession);
    D(printf("-->%s/%s\n", m_lastDataSession.toString().toAscii().data(), m_lastDataSystem.toString().toAscii().data()));
    D(QStringListIterator dataIterator(m_lastDataSession);
    while (dataIterator.hasNext()) {
    QString datum =  dataIterator.next();
        printf("-->%s\n", datum.toAscii().data());
    }
     );
    setData(SOURCE_SESSION_SERVICES,  I18N_NOOP("services"), QVariant(m_lastDataSession));
    setData(SOURCE_SYSTEM_SERVICES,  I18N_NOOP("services"), QVariant(m_lastDataSystem));
    D(printf("Data set\n"));
}

K_EXPORT_PLASMA_DATAENGINE(dbus, DBusEngine)

#include "dbusengine.moc"
